#include "servicewidget.h"
#include "service/httplikeservice.h"
#include "service/activeservicemanager.h"

#include "utils/logger.h"


ServiceWidget::ServiceWidget(QWidget *parent)
:QDockWidget(parent)
{
    setupUi(this);
    m_httpServer = new HttpLikeService();
    LOG_DEBUG("add service into mananger");
    ActiveServiceManager::getInstance()->addRunningService(m_httpServer);

    m_runningFlag = false;
    LOG_DEBUG("Set text service widget");
    this->blogStartPushButton->setText(QString::fromUtf8("Start Web Server"));
    LOG_DEBUG("Set signal service widget");
    connect(this->blogStartPushButton, SIGNAL(clicked()), this, SLOT(doAction()));
    LOG_DEBUG("end init service widget");
}

ServiceWidget::~ServiceWidget()
{
    ActiveServiceManager::getInstance()->removeRunningService(m_httpServer->getServiceName());
    if(m_httpServer) delete m_httpServer;

}

void ServiceWidget::doAction()
{
    LOG_DEBUG("start do action");
    if(!m_runningFlag)
    {
        startHttpServer();
        this->blogStartPushButton->setText(QString::fromUtf8("Stop Web Server"));
        m_runningFlag = true;
    }
    else
    {
        stopHttpServer();
        this->blogStartPushButton->setText(QString::fromUtf8("Start Web Server"));
        m_runningFlag = false;
    }
}

void ServiceWidget::startHttpServer()
{
    m_httpServer->start();
    m_runningFlag = true;
}

void ServiceWidget::stopHttpServer()
{
    m_httpServer->stop();
    m_runningFlag = false;
}

bool ServiceWidget::isHttpServerRunning()
{
    return m_httpServer->isServerRunning();
}
